package chapter_14_chain_of_responsibility.classic;

import chapter_14_chain_of_responsibility.classic.base.Level;
import chapter_14_chain_of_responsibility.classic.base.Request;
import chapter_14_chain_of_responsibility.classic.base.Response;

/**
 * Created by CharloYao on 2017/9/29.
 * 抽象处理类
 */
public abstract class Handler {

    //下一个处理者
    private Handler nextHandler;

    //处理方法，给客户端调用
    public Response handle(Request request){
        Response response = null;

        //当前处理者能处理请求的等级
        if(this.getHandlerLevel() == request.getRequestLevel()){
            response = echo(request);
        }else{
            if(nextHandler != null){
                response = this.nextHandler.handle(request);            //让下一个处理者来处理
            }else{
                //没有下一个处理者，可以实现自己的逻辑
            }
        }
        return response;
    }

    public void setNextHandler(Handler handler){
        this.nextHandler = handler;
    }

    //获得能处理的lvel
    protected abstract Level getHandlerLevel();

    //处理后，返回response
    protected abstract Response echo(Request request);
}
